package com.example.lzy.listviewsample.com.example.lzy.listviewsample.zdy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lzy.listviewsample.R;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by lzy on 2018/4/9.
 */

public class MyUpdateAdapter extends BaseAdapter {

   private Context mContext;
   private List<UpdateData> mUpdateData;

   public MyUpdateAdapter(){}

    public MyUpdateAdapter(Context context,List<UpdateData> updateData){
        this.mContext = context;
        this.mUpdateData = updateData;
    }

    /**
     *   添加
     * @param position
     * @param updateData
     */
    public void add(int position,UpdateData updateData) {
        if (null == mUpdateData) {
            mUpdateData = new LinkedList<>();
        }
        mUpdateData.add(position,updateData);
        notifyDataSetChanged();
    }

    /**
     * 更新列表内容
     * @param updateData
     */
    public void update(List<UpdateData> updateData){
        if (null == mUpdateData){
            mUpdateData = new LinkedList<>();
        }
        mUpdateData.clear();
        mUpdateData.addAll(updateData);
        notifyDataSetChanged();
    }

    /**
     * 更新列表项
     */
    public void update(int position,UpdateData updateData){
        if (mUpdateData != null && position < mUpdateData.size()){
            mUpdateData.set(position,updateData);
        }
        notifyDataSetChanged();
    }

    /**
     * 移除指定列表项
     * @param position
     */
    public void remove (int position){
        if (mUpdateData != null && 0 != getCount()){
            mUpdateData.remove(position);
        }
        notifyDataSetChanged();
    }

    /**
     * 清空列表数据
     */
    public  void clear(){
        if (mUpdateData != null){
            mUpdateData.clear();
        }
        notifyDataSetChanged();

    }




    @Override
    public int getCount() {
        return mUpdateData.size();
    }

    @Override
    public UpdateData getItem(int i) {
        return mUpdateData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (null == convertView){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.updatedata_item,null);
            viewHolder = new ViewHolder();
            viewHolder.img_icon = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        UpdateData updateData =  getItem(position);
        viewHolder.img_icon.setImageResource(updateData.getImgId());
        viewHolder.textView.setText(updateData.getContent());


        return convertView;
    }
    private class ViewHolder{
        ImageView img_icon;
        TextView textView;
    }
}
